// noinspection JSUnresolvedReference

/**
 * 从 window 的 href 拿到名称
 *
 * @author lihh
 */
function getNameByHref(key) {
    const params = jsMethods.queryURLParams(window.location.href);
    return params[key];
}

/**
 * 动态生成 dom
 *
 * @author lihh
 * @param name 传递的名称
 * @return Promise<String>
 */
function dynamicCreateDOM(name) {
    return new Promise(resolve => {
        const divIdNAME = `video-pre-${name}`;

        // 查询 dom 如果已经存在的话，直接返回
        let el = document.getElementById(divIdNAME);
        if (jsMethods.isNotEmpty(el)) {
            resolve(divIdNAME);
            return;
        }

        // 此时就应该创建dom
        el = document.createElement("div");
        el.setAttribute("id", divIdNAME);
        el.setAttribute("class", "player")
        // 将元素添加到 body中
        document.body.appendChild(el);

        resolve(divIdNAME);
    });
}

/**
 * 创建 播放器
 *
 * @author lihh
 * @param elName 这是dom 名称
 */
function createPlayer(elName) {
    const container = "#" + elName;
    // 构建 播放器参数
    const videoObject = {
        container,
        autoplay: false,
        loop: true,
        muted: true,
        jsonPath: "./public/json/",
        rightBar: null,
        screenshot: false,
        playbackrateOpen: false,
        webFull: true,
        video: `/public/video/${getNameByHref("name")}.mp4`,
        // video: "http://10.100.33.222/public/preview/resource/1701225045633_2、森园区页面与操作.mp4"
    };
    new ckplayer(videoObject);

    // 查询 video元素
    const videoHTML = document.querySelector(`${container} video`);
    videoHTML.play();

    const divHTML = document.querySelector(`${container} > div`);
    if (jsMethods.isNotEmpty(divHTML)) {
        const allElement = divHTML.querySelectorAll("div");

        for (let i = 0; i < 5; i++) {
            allElement[i].style.display = "none";
        }
    }
}

(function () {
    dynamicCreateDOM(getNameByHref("name")).then(realName => {
        createPlayer(realName)
    });
})();